package StudyCode.summary.BufferAndChannel;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.EnumSet;
import java.util.Set;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CreateFilePrmissionsUnix {
	public static void main(String[] args) {
		Path path = Paths.get("home/folder1/email", "email.txt");
		ByteBuffer buffer = ByteBuffer.wrap("测试 ".getBytes());

		Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-r------");
		FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);

		try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(path,
				EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.APPEND), attr)) {

			int write = seekableByteChannel.write(buffer);
			System.out.println("写入字节数: " + write);

		} catch (IOException ex) {
			System.err.println(ex);
		}
		buffer.clear();
	}
}
